<?php

/**
 *网站配置控制器
 */
class ConfigControl extends Control {

    //基本配置视图
    function base() {
//        $xml = "./template/default/read.xml";
//        p(Xml::toArray(file_get_contents($xml)));
        $tplDirs = dir::tree("./template"); //模板风格        
        $tpls = array(); //模板风格信息
        $id = 0;
        foreach ($tplDirs as $tpl) {

            $xml = $tpl['path'] . '/read.xml';
            $tpls[$id] = array();
            if (is_file($xml)) {
                //获得模板风格的URL地址，用于显示模板图片
                $tplUrl = __ROOT__.'/template/'.basename($tpl['path']);
                //获得模板风格的XML定义
                $tpls[$id] = xml::toArray(file_get_contents($xml));
                //模板截图
                $tpls[$id]['short']=$tplUrl .'/'.basename($tpls[$id]['short']);
                //模板风格名称
                $tpls[$id]['style']=basename($tpl['path']);
            }
            $id++;
        }
        $this->assign("defaultjpg",__ROOT__.'/static/default.png');
        //分配置所有模板风格数据
        $this->assign('tpl_styles',$tpls);
        $db = M("config");
        //获得除了模板风格配置的配置项
        $base_config = $db->where("name<>'style'")->all();
        //获得模板配置
        $tpl_config = $db->where("name='style'")->find();
        $this->assign("base_config", $base_config);
        $this->assign("tpl_config", $tpl_config);
        $this->display();
    }
    //修改配置,先写入数据库再写入配置文件
    function edit(){
        $data = $_POST;
        $db = M('config');
         foreach($data as $name=>$value){
            $sql = "UPDATE ".C('DB_PREFIX')."config SET value='$value'
                    WHERE name='$name'";
            $db->exe($sql);
         }
         $allConfig = $db->field("name,value")->all();
         $dataConfigFIle = array();
         foreach($allConfig  as $conf){
             $dataConfigFIle[$conf['name']]=$conf['value'];
         }
         $configPath = "./config/base.php";
         $config =include $configPath;
         $result = array_merge($config,$dataConfigFIle);
         
         $str = "<?php if(!defined('PATH_HD'))exit;\nreturn ".var_export($result,true).";\n?>";
         if(@file_put_contents("./config/base.php", $str)){
            $this->success("修改配置项成功 ");
         }else{
             $this->error("配置文件不能写，请修改文件权限");
         }
    }
    //验证码配置视图
    function verify(){
        $config =include "./config/base.php";
            if($config){
                $verifyCode = array();
               foreach($config as $key=>$value){
                if(substr($key, 0,4)=='CODE'){
                    $verifyCode[$key] = $value;
                }
            }
            $this->assign("verifyCode",$verifyCode);
            $this->display(); 
        }
    }
    //修改验证码
    function editVerify(){
        if(isset($_POST['CODE_WIDTH'])){
            $verifyCode = $_POST;
            $configPath = "./config/base.php";
            $config =include $configPath;
            $result = array_merge($config,$verifyCode);
            if(@file_put_contents($configPath, "<?php\n if (!defined('PATH_HD')) exit('No direct script access allowed');
                \nreturn ".var_export($result,true).";\n?>")){
                $this->success("修改验证码配置成功！","verify");
            }else{
                $this->error("修改验证码配置失败，请重试！","verify");
            }
        }
    }
    //水印配置视图
    function water(){
        $config =include "./config/base.php";
            if($config){
                $waterConfig = array();
               foreach($config as $key=>$value){
                if(substr($key, 0, 5)=='WATER'){
                    $waterConfig[$key] = $value;
                }
            }
            $this->assign("water",$waterConfig);
            $this->display(); 
        }
    }
    //修改水印配置
    function editWater(){
        if(isset($_POST['WATER_ON'])){
            $waterConfig = $_POST;
            $configPath = "./config/base.php";
            $config =include $configPath;
            $result = array_merge($config,$waterConfig);
            if(@file_put_contents($configPath, "<?php\n if (!defined('PATH_HD')) exit('No direct script access allowed');
                \nreturn ".var_export($result,true).";\n?>")){
                $this->success("修改验证码配置成功！","water");
            }else{
                $this->error("修改验证码配置失败，请重试！","water");
            }
        }
    }

}

?>
